1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.google.common.primitives;
18
19 import com.google.common.annotations.GwtCompatible;
20 import com.google.common.annotations.GwtIncompatible;
21 import com.google.common.collect.testing.Helpers;
22 import com.google.common.testing.NullPointerTester;
23 import com.google.common.testing.SerializableTester;
24
25 import junit.framework.TestCase;
26
27 import java.util.Arrays;
28 import java.util.Collection;
29 import java.util.Collections;
30 import java.util.Comparator;
31 import java.util.List;
32
33
34
35
36
37
38 @GwtCompatible(emulated = true)
39 public class BooleansTest extends TestCase {
40 private static final boolean[] EMPTY = {};
41 private static final boolean[] ARRAY_FALSE = {false};
42 private static final boolean[] ARRAY_TRUE = {true};
43 private static final boolean[] ARRAY_FALSE_FALSE = {false, false};
44 private static final boolean[] ARRAY_FALSE_TRUE = {false, true};
45
46 private static final boolean[] VALUES = {false, true};
47
48 public void testHashCode() {
49 assertEquals(Boolean.TRUE.hashCode(), Booleans.hashCode(true));
50 assertEquals(Boolean.FALSE.hashCode(), Booleans.hashCode(false));
51 }
52
53 public void testCompare() {
54 for (boolean x : VALUES) {
55 for (boolean y : VALUES) {
56
57 assertEquals(x + ", " + y,
58 Boolean.valueOf(x).compareTo(y),
59 Booleans.compare(x, y));
60 }
61 }
62 }
63
64 public void testContains() {
65 assertFalse(Booleans.contains(EMPTY, false));
66 assertFalse(Booleans.contains(ARRAY_FALSE, true));
67 assertTrue(Booleans.contains(ARRAY_FALSE, false));
68 assertTrue(Booleans.contains(ARRAY_FALSE_TRUE, false));
69 assertTrue(Booleans.contains(ARRAY_FALSE_TRUE, true));
70 }
71
72 public void testIndexOf() {
73 assertEquals(-1, Booleans.indexOf(EMPTY, ARRAY_FALSE));
74 assertEquals(-1, Booleans.indexOf(ARRAY_FALSE, ARRAY_FALSE_TRUE));
75 assertEquals(0, Booleans.indexOf(ARRAY_FALSE_FALSE, ARRAY_FALSE));
76 assertEquals(0, Booleans.indexOf(ARRAY_FALSE, ARRAY_FALSE));
77 assertEquals(0, Booleans.indexOf(ARRAY_FALSE_TRUE, ARRAY_FALSE));
78 assertEquals(1, Booleans.indexOf(ARRAY_FALSE_TRUE, ARRAY_TRUE));
79 assertEquals(0, Booleans.indexOf(ARRAY_TRUE, new boolean[0]));
80 }
81
82 public void testIndexOf_arrays() {
83 assertEquals(-1, Booleans.indexOf(EMPTY, false));
84 assertEquals(-1, Booleans.indexOf(ARRAY_FALSE, true));
85 assertEquals(-1, Booleans.indexOf(ARRAY_FALSE_FALSE, true));
86 assertEquals(0, Booleans.indexOf(ARRAY_FALSE, false));
87 assertEquals(0, Booleans.indexOf(ARRAY_FALSE_TRUE, false));
88 assertEquals(1, Booleans.indexOf(ARRAY_FALSE_TRUE, true));
89 assertEquals(2, Booleans.indexOf(new boolean[] {false, false, true}, true));
90 }
91
92 public void testLastIndexOf() {
93 assertEquals(-1, Booleans.lastIndexOf(EMPTY, false));
94 assertEquals(-1, Booleans.lastIndexOf(ARRAY_FALSE, true));
95 assertEquals(-1, Booleans.lastIndexOf(ARRAY_FALSE_FALSE, true));
96 assertEquals(0, Booleans.lastIndexOf(ARRAY_FALSE, false));
97 assertEquals(0, Booleans.lastIndexOf(ARRAY_FALSE_TRUE, false));
98 assertEquals(1, Booleans.lastIndexOf(ARRAY_FALSE_TRUE, true));
99 assertEquals(2, Booleans.lastIndexOf(new boolean[] {false, true, true}, true));
100 }
101
102 public void testConcat() {
103 assertTrue(Arrays.equals(EMPTY, Booleans.concat()));
104 assertTrue(Arrays.equals(EMPTY, Booleans.concat(EMPTY)));
105 assertTrue(Arrays.equals(EMPTY, Booleans.concat(EMPTY, EMPTY, EMPTY)));
106 assertTrue(Arrays.equals(ARRAY_FALSE, Booleans.concat(ARRAY_FALSE)));
107 assertNotSame(ARRAY_FALSE, Booleans.concat(ARRAY_FALSE));
108 assertTrue(Arrays.equals(ARRAY_FALSE, Booleans.concat(EMPTY, ARRAY_FALSE, EMPTY)));
109 assertTrue(Arrays.equals(
110 new boolean[] {false, false, false},
111 Booleans.concat(ARRAY_FALSE, ARRAY_FALSE, ARRAY_FALSE)));
112 assertTrue(Arrays.equals(
113 new boolean[] {false, false, true},
114 Booleans.concat(ARRAY_FALSE, ARRAY_FALSE_TRUE)));
115 }
116
117 public void testEnsureCapacity() {
118 assertSame(EMPTY, Booleans.ensureCapacity(EMPTY, 0, 1));
119 assertSame(ARRAY_FALSE, Booleans.ensureCapacity(ARRAY_FALSE, 0, 1));
120 assertSame(ARRAY_FALSE, Booleans.ensureCapacity(ARRAY_FALSE, 1, 1));
121 assertTrue(Arrays.equals(
122 new boolean[] {true, false, false},
123 Booleans.ensureCapacity(new boolean[] {true}, 2, 1)));
124 }
125
126 public void testEnsureCapacity_fail() {
127 try {
128 Booleans.ensureCapacity(ARRAY_FALSE, -1, 1);
129 fail();
130 } catch (IllegalArgumentException expected) {
131 }
132 try {
133
134 Booleans.ensureCapacity(ARRAY_FALSE, 1, -1);
135 fail();
136 } catch (IllegalArgumentException expected) {
137 }
138 }
139
140 public void testJoin() {
141 assertEquals("", Booleans.join(",", EMPTY));
142 assertEquals("false", Booleans.join(",", ARRAY_FALSE));
143 assertEquals("false,true", Booleans.join(",", false, true));
144 assertEquals("falsetruefalse",
145 Booleans.join("", false, true, false));
146 }
147
148 public void testLexicographicalComparator() {
149 List<boolean[]> ordered = Arrays.asList(
150 new boolean[] {},
151 new boolean[] {false},
152 new boolean[] {false, false},
153 new boolean[] {false, true},
154 new boolean[] {true},
155 new boolean[] {true, false},
156 new boolean[] {true, true},
157 new boolean[] {true, true, true});
158
159 Comparator<boolean[]> comparator = Booleans.lexicographicalComparator();
160 Helpers.testComparator(comparator, ordered);
161 }
162
163 @GwtIncompatible("SerializableTester")
164 public void testLexicographicalComparatorSerializable() {
165 Comparator<boolean[]> comparator = Booleans.lexicographicalComparator();
166 assertSame(comparator, SerializableTester.reserialize(comparator));
167 }
168
169 public void testToArray() {
170
171 List<Boolean> none = Arrays.<Boolean>asList();
172 assertTrue(Arrays.equals(EMPTY, Booleans.toArray(none)));
173
174 List<Boolean> one = Arrays.asList(false);
175 assertTrue(Arrays.equals(ARRAY_FALSE, Booleans.toArray(one)));
176
177 boolean[] array = {false, false, true};
178
179 List<Boolean> three = Arrays.asList(false, false, true);
180 assertTrue(Arrays.equals(array, Booleans.toArray(three)));
181
182 assertTrue(Arrays.equals(array, Booleans.toArray(Booleans.asList(array))));
183 }
184
185 public void testToArray_threadSafe() {
186
187 boolean[] VALUES = BooleansTest.VALUES;
188 VALUES = Booleans.concat(VALUES, VALUES);
189
190 for (int delta : new int[] { +1, 0, -1 }) {
191 for (int i = 0; i < VALUES.length; i++) {
192 List<Boolean> list = Booleans.asList(VALUES).subList(0, i);
193 Collection<Boolean> misleadingSize =
194 Helpers.misleadingSizeCollection(delta);
195 misleadingSize.addAll(list);
196 boolean[] arr = Booleans.toArray(misleadingSize);
197 assertEquals(i, arr.length);
198 for (int j = 0; j < i; j++) {
199 assertEquals(VALUES[j], arr[j]);
200 }
201 }
202 }
203 }
204
205 public void testToArray_withNull() {
206 List<Boolean> list = Arrays.asList(false, true, null);
207 try {
208 Booleans.toArray(list);
209 fail();
210 } catch (NullPointerException expected) {
211 }
212 }
213
214 public void testAsListIsEmpty() {
215 assertTrue(Booleans.asList(EMPTY).isEmpty());
216 assertFalse(Booleans.asList(ARRAY_FALSE).isEmpty());
217 }
218
219 public void testAsListSize() {
220 assertEquals(0, Booleans.asList(EMPTY).size());
221 assertEquals(1, Booleans.asList(ARRAY_FALSE).size());
222 assertEquals(2, Booleans.asList(ARRAY_FALSE_TRUE).size());
223 }
224
225 public void testAsListIndexOf() {
226 assertEquals(-1, Booleans.asList(EMPTY).indexOf("wrong type"));
227 assertEquals(-1, Booleans.asList(EMPTY).indexOf(true));
228 assertEquals(-1, Booleans.asList(ARRAY_FALSE).indexOf(true));
229 assertEquals(0, Booleans.asList(ARRAY_FALSE).indexOf(false));
230 assertEquals(1, Booleans.asList(ARRAY_FALSE_TRUE).indexOf(true));
231 }
232
233 public void testAsListLastIndexOf() {
234 assertEquals(-1, Booleans.asList(EMPTY).indexOf("wrong type"));
235 assertEquals(-1, Booleans.asList(EMPTY).indexOf(true));
236 assertEquals(-1, Booleans.asList(ARRAY_FALSE).lastIndexOf(true));
237 assertEquals(1, Booleans.asList(ARRAY_FALSE_TRUE).lastIndexOf(true));
238 assertEquals(1, Booleans.asList(ARRAY_FALSE_FALSE).lastIndexOf(false));
239 }
240
241 public void testAsListContains() {
242 assertFalse(Booleans.asList(EMPTY).contains("wrong type"));
243 assertFalse(Booleans.asList(EMPTY).contains(true));
244 assertFalse(Booleans.asList(ARRAY_FALSE).contains(true));
245 assertTrue(Booleans.asList(ARRAY_TRUE).contains(true));
246 assertTrue(Booleans.asList(ARRAY_FALSE_TRUE).contains(false));
247 assertTrue(Booleans.asList(ARRAY_FALSE_TRUE).contains(true));
248 }
249
250 public void testAsListEquals() {
251 assertEquals(Booleans.asList(EMPTY), Collections.emptyList());
252 assertEquals(Booleans.asList(ARRAY_FALSE), Booleans.asList(ARRAY_FALSE));
253 assertFalse(Booleans.asList(ARRAY_FALSE).equals(ARRAY_FALSE));
254 assertFalse(Booleans.asList(ARRAY_FALSE).equals(null));
255 assertFalse(Booleans.asList(ARRAY_FALSE).equals(Booleans.asList(ARRAY_FALSE_TRUE)));
256 assertFalse(Booleans.asList(ARRAY_FALSE_FALSE).equals(Booleans.asList(ARRAY_FALSE_TRUE)));
257 assertEquals(1, Booleans.asList(ARRAY_FALSE_TRUE).lastIndexOf(true));
258 List<Boolean> reference = Booleans.asList(ARRAY_FALSE);
259 assertEquals(Booleans.asList(ARRAY_FALSE), reference);
260 assertEquals(reference, reference);
261 }
262
263 public void testAsListHashcode() {
264 assertEquals(1, Booleans.asList(EMPTY).hashCode());
265 assertEquals(Booleans.asList(ARRAY_FALSE).hashCode(), Booleans.asList(ARRAY_FALSE).hashCode());
266 List<Boolean> reference = Booleans.asList(ARRAY_FALSE);
267 assertEquals(Booleans.asList(ARRAY_FALSE).hashCode(), reference.hashCode());
268 }
269
270 public void testAsListToString() {
271 assertEquals("[false]", Booleans.asList(ARRAY_FALSE).toString());
272 assertEquals("[false, true]", Booleans.asList(ARRAY_FALSE_TRUE).toString());
273 }
274
275 public void testAsListSet() {
276 List<Boolean> list = Booleans.asList(ARRAY_FALSE);
277 assertFalse(list.set(0, true));
278 assertTrue(list.set(0, false));
279 try {
280 list.set(0, null);
281 fail();
282 } catch (NullPointerException expected) {
283 }
284 try {
285 list.set(1, true);
286 fail();
287 } catch (IndexOutOfBoundsException expected) {
288 }
289 }
290
291 public void testCountTrue() {
292 assertEquals(0, Booleans.countTrue());
293 assertEquals(0, Booleans.countTrue(false));
294 assertEquals(1, Booleans.countTrue(true));
295 assertEquals(3, Booleans.countTrue(false, true, false, true, false, true));
296 assertEquals(1, Booleans.countTrue(false, false, true, false, false));
297 }
298
299 @GwtIncompatible("NullPointerTester")
300 public void testNulls() {
301 new NullPointerTester().testAllPublicStaticMethods(Booleans.class);
302 }
303 }